package com.example.hilt_mvvm_http.dialog

import android.graphics.Color
import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.util.DisplayMetrics
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.example.hilt_mvvm_http.R
import com.example.hilt_mvvm_http.dialog.MessageNoticeDialog.Companion

class AppUpdateDialog(
    private val title:String? = null,
    private val message: String? = null,
    private val force:Boolean = false, //是否强制，若强制不现实取消按钮
    private val onUpdateClick:(()->Unit)? = null
) :DialogFragment(){
    companion object{
        val TAG:String = AppUpdateDialog::class.java.simpleName
    }
    private lateinit var mParentView: View //对话框的view
    //设置样式
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.MessageNoticeDialog)
    }

    //设置窗体属性
    override fun onStart() {
        super.onStart()
        //对话框自己窗体属性
        val window = dialog!!.window
        val wlp = window!!.attributes
        //activity窗体属性
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        //将对话框的窗体属性重新赋值成activity的0.72倍数
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        wlp.gravity = Gravity.CENTER
        wlp.width = (dm.widthPixels * 0.72).toInt()
        window.attributes = wlp
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mParentView = layoutInflater.inflate(R.layout.dialog_app_update,container,false)
        return mParentView
    }

    /**
     * 设置内容
     *      private val title:String? = null,
     *     private val message: String? = null,
     *     private val force:Boolean = false, //是否强制，若强制不现实取消按钮
     *     private val onUpdateClick:(()->Unit)? = null
     */

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)
        mParentView.findViewById<TextView>(R.id.tv_title).text = title
        mParentView.findViewById<TextView>(R.id.tv_message).text = message
        //取消按钮
        if(force){
            mParentView.findViewById<ImageView>(R.id.tv_cancel).visibility = View.GONE
        }else{
            mParentView.findViewById<ImageView>(R.id.tv_cancel).apply {
                visibility = View.VISIBLE
                setOnClickListener {
                    dismiss()
                }

            }
        }
        //下载按钮
        mParentView.findViewById<TextView>(R.id.btn_update).setOnClickListener {
            onUpdateClick?.invoke()
            dismiss()
        }


    }

    /**
     * 显示对话框
     */
    fun show(fragmentManager: FragmentManager){
        Log.d(MessageNoticeDialog.TAG, "show: ")
        if(fragmentManager.findFragmentByTag(MessageNoticeDialog.TAG)?.isVisible == true){//判断当前对话框是否显示
            return
        }
        this.show(fragmentManager, MessageNoticeDialog.TAG)

    }



}